package com.prp.gwt.server;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import com.prp.bo.Friend;
import com.prp.bo.MessageBoardBO;
import com.prp.bo.Recommendation;
import com.prp.bo.SubjectRecommend;
import com.prp.bo.User;
import com.prp.bo.UserStateBO;
import com.prp.db.po.Msgboard;
import com.prp.gwt.client.ClientConstants;
import com.prp.gwt.client.ClientException;
import com.prp.gwt.client.user.php.PHPService;
import com.prp.gwt.client.vo.Message;
import com.prp.gwt.client.vo.MessageList;
import com.prp.gwt.client.vo.SubjectRecommendList;
import com.prp.gwt.client.vo.UserList;
import com.prp.gwt.client.vo.UserState;
import com.prp.gwt.client.vo.UserStateList;

public class PHPServiceImpl extends GlobalServiceImpl implements PHPService {

	private static final long serialVersionUID = -4121384202338844640L;

	public static UserState parseUserState(com.prp.db.po.UserState userState) {
		UserState us = new UserState();
		if (userState != null) {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			us.setDate(df.format(userState.getDate()));
			us.setState(userState.getState());
		} else {
			us.setDate(null);
			us.setState(null);
		}
		return us;
	}

	public static Message parseMessage(Msgboard msg) {
		Message message = new Message();
		message.setId(msg.getMsgId());
		message.setFromUser(User.parse(User.getUser(msg.getMsgFromId())));
		message.setToUser(User.parse(User.getUser(msg.getMsgToId())));
		message.setInUser(User.parse(User.getUser(msg.getMsgInId())));
		message.setContent(msg.getContent());
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		message.setDate(df.format(msg.getDate()));
		return message;
	}

	@Override
	public UserStateList getLatestState(int id) throws ClientException {
		validateAuth(ClientConstants.USER);
		int currentUser = (Integer) getThreadLocalRequest().getSession().getAttribute(
				ClientConstants.ATTR_USER);
		if (id == -1){
			id = currentUser;
		}
		com.prp.gwt.client.vo.User user = User.parse(User.getUser(id));
		user.setNeighbour(Friend.isNeighbour(id, currentUser));
		UserStateList stateList = new UserStateList();
		stateList.addUserState(parseUserState(UserStateBO.getLatestState(id)));
		user.isNeighbour();
		stateList.setOwner(user);
		return stateList;
	}

	@Override
	public void addState(String content) throws ClientException {
		validateAuth(ClientConstants.USER);
		int userId = (Integer) getThreadLocalRequest().getSession()
				.getAttribute(ClientConstants.ATTR_USER);
		UserStateBO.addState(content, userId);
	}

	@Override
	public UserStateList getStateList(int userId) throws ClientException {
		validateAuth(ClientConstants.USER);
		UserStateList userStateList = new UserStateList();
		if (userId == -1) {
			userStateList.setOwner(null);
			userId = (Integer) getThreadLocalRequest().getSession()
					.getAttribute(ClientConstants.ATTR_USER);
		} else
			userStateList.setOwner(User.parse(User.getUser(userId)));
		ArrayList<com.prp.db.po.UserState> list = UserStateBO.getState(userId);
		for (com.prp.db.po.UserState state : list)
			userStateList.addUserState(parseUserState(state));
		return userStateList;
	}

	@Override
	public UserList getUserRecommendation() throws ClientException {
		validateAuth(ClientConstants.USER);
		int userId = (Integer) getThreadLocalRequest().getSession()
				.getAttribute(ClientConstants.ATTR_USER);
		ArrayList<Integer> list = Recommendation.recommend(userId);
		UserList userList = new UserList();
		for (int i = 0; i < list.size() && i < 9; i++)
			userList.addUser(User.parse(User.getUser(list.get(i))));
		return userList;
	}

	@Override
	public void addMessage(String content, int toUserId, int inUserId)
			throws ClientException {
		validateAuth(ClientConstants.USER);
		int userId = (Integer) getThreadLocalRequest().getSession()
				.getAttribute(ClientConstants.ATTR_USER);
		MessageBoardBO.addMessage(content, userId, toUserId, inUserId);
	}

	@Override
	public MessageList getMessageList(int userId) throws ClientException {
		validateAuth(ClientConstants.USER);
		int currentUserId = (Integer) getThreadLocalRequest().getSession()
				.getAttribute(ClientConstants.ATTR_USER);
		if (userId == -1) {
			userId = currentUserId;
		}
		ArrayList<Msgboard> list = MessageBoardBO.getMessageByInUserId(userId);
		MessageList msgList = new MessageList();
		for (Msgboard msg : list)
			msgList.add(parseMessage(msg));
		msgList.setUser(User.parse(User.getUser(userId)));
		msgList.setCurrentUser(User.parse(User.getUser(currentUserId)));
		return msgList;
	}

	@Override
	public void deleteMessage(int id) throws ClientException {
		validateAuth(ClientConstants.USER);
		int userId = (Integer) getThreadLocalRequest().getSession()
				.getAttribute(ClientConstants.ATTR_USER);
		MessageBoardBO.deleteMessage(userId, id);
	}

	@Override
	public MessageList getUncheckedMessageList() throws ClientException {
		validateAuth(ClientConstants.USER);
		int userId = (Integer) getThreadLocalRequest().getSession()
				.getAttribute(ClientConstants.ATTR_USER);
		ArrayList<Msgboard> list = MessageBoardBO.findByUnchecked(userId);
		MessageList msgList = new MessageList();
		for (Msgboard msg : list)
			msgList.add(parseMessage(msg));
		msgList.setCurrentUser(User.parse(User.getUser(userId)));
		return msgList;
	}

	@Override
	public void setMessageChecked(int id) throws ClientException {
		validateAuth(ClientConstants.USER);
		int userId = (Integer) getThreadLocalRequest().getSession()
				.getAttribute(ClientConstants.ATTR_USER);
		MessageBoardBO.setChecked(id, userId);
	}

	@Override
	public SubjectRecommendList getArticalRecommendation()
			throws ClientException {
		validateAuth(ClientConstants.USER);
		int userId = (Integer) getThreadLocalRequest().getSession()
				.getAttribute(ClientConstants.ATTR_USER);
		return SubjectRecommend.getUserSubjectRecommend(userId);
	}

	@Override
	public SubjectRecommendList getRecentRecommendation(int userId)
			throws ClientException {
		validateAuth(ClientConstants.USER);
		SubjectRecommendList list = null;
		if (userId == -1) {
			userId = (Integer) getThreadLocalRequest().getSession()
					.getAttribute(ClientConstants.ATTR_USER);
			list = SubjectRecommend.getUserRecentRecommend(userId);
			list.setOwner(null);
		} else {
			list = SubjectRecommend.getUserRecentRecommend(userId);
			list.setOwner(User.parse(User.getUser(userId)));
		}
		return list;
	}

}
